<template>
  <div class="Con">
    <div class="Con-H">
      <el-select
        v-model="roomValue"
        placeholder="选择课室"
        size="small"
        style="width: 20%; margin-right: 10px; margin-left: 10px"
      >
        <el-option v-for="item in rooms" :key="item" :value="item"> </el-option>
      </el-select>

      <el-input
        v-model="AdminName"
        placeholder="请输入管理员姓名"
        size="small"
        style="width: 20%; margin-right: 10px"
      ></el-input>
      <el-button type="primary" size="small" @click="inputAdmin()"
        >录入<i class="el-icon-upload el-icon--right"></i
      ></el-button>
    </div>

    <div class="Con-C">
      <el-table :data="tableData" height="100%" border style="width: 100%">
        <el-table-column prop="room" label="课室" sortable> </el-table-column>
        <el-table-column prop="name" label="管理员"> </el-table-column>
        <el-table-column prop="telephone" label="联系方式"> </el-table-column>

        <el-table-column label="教师签名" prop="tpicture">
          <template slot-scope="scope"
            ><!--slot--->
            <img
              :src="scope.row.tpicture"
              style="
                width: 75%;
                height: 70px;
                box-shadow: 1px 1px 5px rgb(146, 146, 146);
              "
              alt="image"
            />
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
              >修改</el-button
            >

            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-dialog title="修改管理员" :visible.sync="dialogVisible" width="30%">
        <span style="font-size: 12px; color: red"
          >*注： 该管理员已经完成注册</span
        >
        <el-input
          v-model="newAdmin"
          placeholder="请输入新管理员的姓名"
          size="small"
          style="margin-top: 10px"
        ></el-input>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" size="small"
            >取 消</el-button
          >
          <el-button type="primary" @click="updateAdmin()" size="small"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { axios1 } from "@/network";
import Qs from "qs";
export default {
  data() {
    return {
      dialogVisible: false,
      roomValue: "",
      AdminName: "",
      rooms: ["", 102, 203, 301, 304, 401, 403, 404, 501, 503, 504],
      tableData: [], //信息
      newAdmin: "", //新管理员
      newRoom: "",
    };
  },
  methods: {
    getRoomAdmin() {
      axios1({
        url: "/roomAdministrator/selectTotalRoomAdministrator",
        method: "post",
        headers: {
          token: this.$store.state.token,
        },
      }).then((res) => {
        this.tableData = res.data.data;
        console.log(res);
      });
    },

    handleEdit(index, row) {
      //编辑操作
      this.newRoom = row.room;
      //console.log(this.newRoom)
      this.dialogVisible = true;
    },

    handleDelete(index, row) {
      //删除
      //console.log(row.room);
      this.$confirm("此操作将永久删除该信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let mydeldata = {
            room: row.room,
          };
          axios1({
            url: "/roomAdministrator/deleteRoomAdministrator",
            method: "post",
            data: Qs.stringify(mydeldata),
            headers: {
              token: this.$store.state.token,
            },
          })
            .then((res) => {
              this.$message({
                type: "success",
                message: "删除成功!",
                duration: 1200,
              });
              setTimeout(() => {
                location.reload();
              }, 1000);
            })
            .catch((res) => {
              alert(res);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
            duration: 1200,
          });
        });
    },
    inputAdmin() {
      //录入管理员
      if (this.roomValue === "" || this.AdminName === "") {
        this.$message({
          message: "信息项不可以为空！",
          type: "warning",
          duration: 1200,
        });
      } else {
        axios1({
          url: "/roomAdministrator/insertRoomAdministrator",
          method: "post",
          headers: {
            token: this.$store.state.token,
          },
          data: {
            room: this.roomValue,
            name: this.AdminName,
          },
        }).then((res) => {
          alert(res.data.msg);
          location.reload();
        });
      }
    },

    updateAdmin() {
      //修改管理员的弹出框事件
      if (this.newAdmin === "") {
        this.$message({
          message: "输入框不可以为空！",
          type: "warning",
          duration: 1200,
        });
      } else {
        let mydatax = {
          room: this.newRoom,
          name: this.newAdmin,
        };
        axios1({
          url: "/roomAdministrator/updateRoomAdministrator",
          method: "post",
          headers: {
            token: this.$store.state.token,
          },
          data: Qs.stringify(mydatax),
        })
          .then((res) => {
            alert(res.data.msg);
            location.reload();
          })
          .catch((res) => {
            alert(res.data.msg);
            location.reload();
          });
        this.dialogVisible = false;
      }
    },
  },
  created() {
    this.getRoomAdmin();
  },
};
</script>

<style scoped>
.Con {
  width: 98%;
  height: 98%;
  margin: auto;
  box-shadow: 1px 1px 10px rgb(110, 109, 109);
  margin-top: 1%;
  overflow: auto;
}

.Con-H {
  height: 10%;
  width: 100%;

  border-bottom: 1px solid rgb(214, 211, 211);
  display: flex;
  align-items: center;
}
.Con-C {
  width: 100%;
  height: 88%;
}
</style>
